﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TimeTracker.Miscellaneous
{
    public class TimeSpanFormatProvider : IFormatProvider, ICustomFormatter
    {
        #region IFormatProvider Members

        object IFormatProvider.GetFormat(Type formatType)
        {
            if (formatType is ICustomFormatter)
            {
                return this;
            }
            return null;
        }

        #endregion

        #region ICustomFormatter Members

        string ICustomFormatter.Format(string format, object arg, IFormatProvider formatProvider)
        {
            if (arg == null) { return String.Empty; }

            if (arg is TimeSpan)
            {
                TimeSpan time = (TimeSpan)arg;

                //return String.Format("{0}:{1:00}:{2:00}", time.TotalHours, time.Minutes, time.Seconds);  Why doesn't this code run?

                return new DateTime(time.Ticks).ToString(format, formatProvider);
            }

            return arg.ToString();
        }

        #endregion
    }
}
